﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Citrix.XaAdminMobile
{
    /// <summary>
    /// Extension methods for eventing.
    /// </summary>
    internal static class EventExtensions
    {
        /// <summary>
        /// Raises the supplied event using the recommended pattern. The set of event handlers
        /// is captured as the variable <paramref name="handler"/> when the function is called,
        /// so we know handler is not going to be mutated between the null check and raising
        /// the event.
        /// </summary>
        /// <param name="handler">The event to raise.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The event args.</param>
        public static void Raise(this EventHandler handler, object sender, EventArgs args)
        {
            if (handler != null)
            {
                handler(sender, args);
            }
        }

        /// <summary>
        /// Raises the supplied event using the recommended pattern. The set of event handlers
        /// is captured as the variable <paramref name="handler"/> when the function is called,
        /// so we know handler is not going to be mutated between the null check and raising
        /// the event.
        /// </summary>
        /// <typeparam name="T">The event arg type.</typeparam>
        /// <param name="handler">The event to raise.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The event args.</param>
        public static void Raise<T>(this EventHandler<T> handler,
          object sender, T args) where T : EventArgs
        {
            if (handler != null)
            {
                handler(sender, args);
            }
        }

        /// <summary>
        /// Raises the supplied event using the recommended pattern. The set of event handlers
        /// is captured as the variable <paramref name="handler"/> when the function is called,
        /// so we know handler is not going to be mutated between the null check and raising
        /// the event.
        /// </summary>
        /// <param name="handler">The event to raise.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The event args.</param>
        public static void Raise(this PropertyChangedEventHandler handler, object sender, PropertyChangedEventArgs args)
        {
            if (handler != null)
            {
                handler(sender, args);
            }
        }
    }
}
